package com.block.account.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.account.enums.AccountTypeEnum;
import com.block.account.enums.ActionTypeEnum;
import com.block.account.factory.AccountTypeFactory;
import com.block.account.model.dto.ChangeAmountDTO;
import com.block.account.model.result.ChangeAmountResult;
import com.block.account.service.IAccountHandleService;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;

@Service
public class AccountHandleService implements IAccountHandleService {

    private Logger log = LogFactory.getAccountLog();

    @Autowired
    NotifyExceptionService notifyExceptionService;

    /**
     * 入账
     * 消耗
     * 生成账单（直接入账）
     * 调账（子账户信息）
     */
    @Override
    @Transactional
    public ChangeAmountResult changeAmountHandle(ChangeAmountDTO changeAmountDTO){

        log.info("【执行入账操作】【changeAmountDTO】{}" , JSON.toJSONString(changeAmountDTO));

        AccountTypeEnum accountTypeEnum = changeAmountDTO.getAccountTypeEnum();
        ChangeAmountResult changeAmountResult = AccountTypeFactory.getService(accountTypeEnum.getValue()).changeAmount(changeAmountDTO);
        log.info("【执行入账操作】执行入账完成【changeAmountResult】{}【changeAmountDTO】{}" , JSON.toJSONString(changeAmountResult), JSON.toJSONString(changeAmountDTO));
        // 进行主账户记录的更新
        if (!changeAmountResult.isSuccess()){
            return changeAmountResult;
        }

        // 充值/退款 操作，需要同步更新总账户的信息
        if (ActionTypeEnum.updateMainAction(changeAmountDTO.getActionTypeEnum().getValue())){
            // 更新主账户的信息
            ChangeAmountDTO changeMainDto = new ChangeAmountDTO();
            changeMainDto.setAccountId(changeAmountDTO.getMainAccountId());

            changeMainDto.setChangeAmount(changeAmountDTO.getChangeAmount());
            changeMainDto.setActionTypeEnum(changeAmountDTO.getActionTypeEnum());
            changeMainDto.setAccountTypeEnum(AccountTypeEnum.ACCOUNT);

            changeMainDto.setDataInfo(changeAmountDTO.getDataInfo());
            changeMainDto.setRemarkInfo(changeAmountDTO.getRemarkInfo());
            changeMainDto.setOperatorId(changeAmountDTO.getOperatorId());
            changeMainDto.setOperatorName(changeAmountDTO.getOperatorName());

            ChangeAmountResult changeMainResult =
                    AccountTypeFactory.getService(AccountTypeEnum.ACCOUNT.getValue())
                    .changeAmount(changeMainDto);

            log.info("【执行入账操作】执行主账户入账完成【changeMainResult】{}【changeMainDto】{}" , JSON.toJSONString(changeMainResult), JSON.toJSONString(changeMainDto));
            if (!changeMainResult.isSuccess()){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return changeMainResult;
            }
        }

        // 是否进行调账的操作
        return changeAmountResult;
    }

}
